/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.hbzhit.modules.act.controller;

import com.hbzhit.common.annotation.SysLog;
import com.hbzhit.common.exception.RRException;
import com.hbzhit.common.utils.R;
import com.hbzhit.common.validator.ValidatorUtils;
import com.hbzhit.common.validator.group.AddGroup;
import com.hbzhit.modules.act.dto.ModelForm;
import com.hbzhit.modules.act.service.ModelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.util.Map;

/**
 * @author lengleng
 * @date 2018/9/25
 */
@Api(tags = {"流程模型接口"})
@Slf4j
@RestController
@RequestMapping("/act/model")
@AllArgsConstructor
public class ModelController {
    private final ModelService modelService;

    @ApiOperation(value = "新增流程模型")
    @SysLog("新增流程模型")
    @PostMapping(value = "/insert")
    public R insertForm(@RequestBody ModelForm form) {
        ValidatorUtils.validateEntity(form, AddGroup.class);
        modelService.create(form.getName(), form.getKey(), form.getDesc(), form.getCategory());
        return R.ok();
    }

    @ApiOperation(value = "流程模型分页数据")
    @GetMapping
    public R getModelPage(@RequestParam Map<String, Object> params) {
        return R.ok(modelService.getModelPage(params));
    }

    @ApiOperation(value = "获取流程模型")
    @DeleteMapping("/{id}")
    public R removeModelById(@PathVariable("id") String id) {
        return R.ok(modelService.removeModelById(id));

    }

    @ApiOperation(value = "部署流程模型")
    @SysLog("部署流程模型")
    @PostMapping("/deploy/{id}")
    public R deploy(@PathVariable("id") String id) {
        return R.ok(modelService.deploy(id));
    }

    @ApiOperation(value = "导出流程模型")
    @GetMapping({"/export/{modelId}/{type}"})
    public void export(@PathVariable("modelId") String modelId, @PathVariable("type") String type, HttpServletResponse response) {
        try {
            Map map = modelService.getBpmn(modelId, type);
            response.setHeader("content-type", "application/force-download");
            response.setContentType("application/force-download");
            response.setHeader("Content-Disposition", "attachment; filename=" + map.get("name"));
            ByteArrayInputStream in1 = new ByteArrayInputStream((byte[]) map.get("byte"));
            IOUtils.copy(in1, response.getOutputStream());
            response.flushBuffer();
        } catch (Exception var13) {
            log.error("导出model的xml文件失败：modelId={}, type={}", new Object[]{modelId, type, var13});
            throw new RRException("获取BPMN失败！");
        }

    }
}
